package com.androidx.p013lv.invention.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.AdInfoBean;
import com.androidx.p013lv.base.bean.LabelBean;
import com.androidx.p013lv.base.bean.LabelData;
import com.androidx.p013lv.base.bean.LabelVideoBean;
import com.androidx.p013lv.base.bean.LabelVideoData;
import com.androidx.p013lv.base.bean.request.ReqLabelVideo;
import com.androidx.p013lv.base.dialog.DialogLoading;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.AdUtils;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.invention.C1069R;
import com.androidx.p013lv.invention.adapter.AdapterLabel;
import com.androidx.p013lv.invention.adapter.AdapterLabelVideoTwo;
import com.androidx.p013lv.invention.databinding.FragmentHomeLabelBinding;
import com.androidx.p013lv.invention.view.gridpager.PagerGridLayoutManager;
import com.androidx.p013lv.invention.view.gridpager.PagerGridSnapHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/* renamed from: com.androidx.lv.invention.fragment.HomeLabelFragment */
public class HomeLabelFragment extends LazyFragment<FragmentHomeLabelBinding> implements OnRefreshLoadMoreListener, View.OnClickListener, PagerGridLayoutManager.PageListener {
    String HTTP_TAG_LABEL = "httpTagLabel";
    String HTTP_TAG_LABEL_VIDEO = "httpTagLabelVideo";

    /* renamed from: a */
    private boolean f248a = true;
    private AdapterLabel adapterLabel;
    AdapterLabelVideoTwo adapterLabelVideoTwo;
    private int classifyId;
    int count = 0;
    private int editMax;
    private View headerView;
    List<Integer> integerList = new ArrayList();
    private boolean isLast;
    private int lastId = 0;
    private PagerGridLayoutManager layoutManager;
    private DialogLoading loading;
    private List<LabelVideoData> mList = new ArrayList();
    private int pageTotalSize;
    private RecyclerView rv_label;
    private int sortBy = 1;
    private TextView tv_hot;
    private TextView tv_like;
    private TextView tv_new;
    private TextView tv_next;

    static /* synthetic */ int access$308(HomeLabelFragment homeLabelFragment) {
        int i = homeLabelFragment.editMax;
        homeLabelFragment.editMax = i + 1;
        return i;
    }

    static /* synthetic */ int access$310(HomeLabelFragment homeLabelFragment) {
        int i = homeLabelFragment.editMax;
        homeLabelFragment.editMax = i - 1;
        return i;
    }

    public static HomeLabelFragment newInstance(int i) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", Integer.valueOf(i));
        HomeLabelFragment homeLabelFragment = new HomeLabelFragment();
        homeLabelFragment.setArguments(bundle);
        return homeLabelFragment;
    }

    @Override // androidx.fragment.app.Fragment
    public void setArguments(Bundle bundle) {
        super.setArguments(bundle);
        if (bundle != null) {
            this.classifyId = bundle.getInt("data");
        }
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C1069R.C1074layout.fragment_home_label;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        DialogLoading dialogLoading = new DialogLoading(getContext());
        this.loading = dialogLoading;
        dialogLoading.setTvHint("拚命載入中...");
        ((FragmentHomeLabelBinding) this.binding).setClick(new OnClick());
        ((FragmentHomeLabelBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentHomeLabelBinding) this.binding).refresh.setOnLoadMoreListener(this);
        ((FragmentHomeLabelBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        this.lastId = 0;
        AdapterLabelVideoTwo adapterLabelVideoTwo2 = new AdapterLabelVideoTwo();
        this.adapterLabelVideoTwo = adapterLabelVideoTwo2;
        adapterLabelVideoTwo2.addChildClickViewIds(C1069R.C1072id.retry_view, C1069R.C1072id.ll_root);
        this.adapterLabelVideoTwo.setOnItemChildClickListener(new OnItemChildClickListener() {
            /* class com.androidx.p013lv.invention.fragment.HomeLabelFragment.C11361 */

            @Override // com.chad.library.adapter.base.listener.OnItemChildClickListener
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (!HomeLabelFragment.this.isOnClick() && view.getId() == C1069R.C1072id.retry_view) {
                    HomeLabelFragment.this.lastId = 0;
                    HomeLabelFragment.this.loading.show();
                    HomeLabelFragment.this.getLabelVideo();
                }
            }
        });
        View inflate = LayoutInflater.from(getActivity()).inflate(C1069R.C1074layout.fragment_home_label_header, (ViewGroup) null);
        this.headerView = inflate;
        this.adapterLabelVideoTwo.addHeaderView(inflate);
        ((FragmentHomeLabelBinding) this.binding).recycler.setAdapter(this.adapterLabelVideoTwo);
        this.adapterLabel = new AdapterLabel();
        PagerGridLayoutManager pagerGridLayoutManager = new PagerGridLayoutManager(4, 5, 1);
        this.layoutManager = pagerGridLayoutManager;
        pagerGridLayoutManager.setPageListener(this);
        RecyclerView recyclerView = (RecyclerView) this.headerView.findViewById(C1069R.C1072id.rv_label);
        this.rv_label = recyclerView;
        recyclerView.setLayoutManager(this.layoutManager);
        new PagerGridSnapHelper().attachToRecyclerView(this.rv_label);
        this.rv_label.setAdapter(this.adapterLabel);
        this.adapterLabel.setItemListener(new ItemClickListener() {
            /* class com.androidx.p013lv.invention.fragment.HomeLabelFragment.C11372 */

            @Override // com.androidx.p013lv.base.listener.ItemClickListener
            public void onItemClick(View view, int i) {
                int tagsId;
                if (!HomeLabelFragment.this.isOnClick()) {
                    if (((LabelData) HomeLabelFragment.this.adapterLabel.getDataInPosition(i)).getEditState() == 0) {
                        HomeLabelFragment.access$308(HomeLabelFragment.this);
                    } else {
                        HomeLabelFragment.access$310(HomeLabelFragment.this);
                    }
                    if (HomeLabelFragment.this.editMax > 5) {
                        HomeLabelFragment.access$310(HomeLabelFragment.this);
                        return;
                    }
                    ((LabelData) HomeLabelFragment.this.adapterLabel.getDataInPosition(i)).setEditState(((LabelData) HomeLabelFragment.this.adapterLabel.getDataInPosition(i)).getEditState() == 0 ? 1 : 0);
                    HomeLabelFragment.this.adapterLabel.notifyDataSetChanged();
                    List data = HomeLabelFragment.this.adapterLabel.getData();
                    HomeLabelFragment.this.integerList.clear();
                    for (int i2 = 0; i2 < data.size(); i2++) {
                        if (1 == ((LabelData) HomeLabelFragment.this.adapterLabel.getDataInPosition(i2)).getEditState() && (tagsId = ((LabelData) data.get(i2)).getTagsId()) != 0) {
                            HomeLabelFragment.this.integerList.add(Integer.valueOf(tagsId));
                        }
                    }
                    HomeLabelFragment.this.lastId = 0;
                    HomeLabelFragment.this.getLabelVideo();
                }
            }
        });
        getLabel(this.classifyId);
        this.tv_next = (TextView) this.headerView.findViewById(C1069R.C1072id.tv_next);
        this.tv_new = (TextView) this.headerView.findViewById(C1069R.C1072id.tv_new);
        this.tv_hot = (TextView) this.headerView.findViewById(C1069R.C1072id.tv_hot);
        this.tv_like = (TextView) this.headerView.findViewById(C1069R.C1072id.tv_like);
        this.headerView.findViewById(C1069R.C1072id.tv_next).setOnClickListener(this);
        this.headerView.findViewById(C1069R.C1072id.tv_new).setOnClickListener(this);
        this.headerView.findViewById(C1069R.C1072id.tv_hot).setOnClickListener(this);
        this.headerView.findViewById(C1069R.C1072id.tv_like).setOnClickListener(this);
    }

    /* access modifiers changed from: package-private */
    public void getLabel(int i) {
        HttpUtils.getInsatance().get(UrlManager.getInsatance().getTagsById(i), new HttpCallback<BaseRes<LabelBean>>(this.HTTP_TAG_LABEL) {
            /* class com.androidx.p013lv.invention.fragment.HomeLabelFragment.C11383 */

            public void onLvSuccess(BaseRes<LabelBean> baseRes) {
                if (HomeLabelFragment.this.binding != null && HomeLabelFragment.this != null && baseRes.getCode() == 200 && baseRes.getData() != null && baseRes.getData().getData() != null && baseRes.getData().getData().size() > 0) {
                    HomeLabelFragment.this.adapterLabel.setData(baseRes.getData().getData());
                    HomeLabelFragment.this.adapterLabel.notifyDataSetChanged();
                    HomeLabelFragment.this.getLabelVideo();
                }
            }
        });
    }

    /* access modifiers changed from: package-private */
    public void getLabelVideo() {
        if (NetUtil.isNetworkAvailable()) {
            if (this.f248a) {
                this.f248a = false;
            } else {
                this.loading.show();
            }
            ReqLabelVideo reqLabelVideo = new ReqLabelVideo();
            reqLabelVideo.setClassifyId(this.classifyId);
            reqLabelVideo.setLastId(this.lastId);
            reqLabelVideo.setPageSize(20);
            reqLabelVideo.setTags(this.integerList);
            reqLabelVideo.setSortBy(this.sortBy);
            String json = new Gson().toJson(reqLabelVideo);
            HttpUtils.getInsatance().post(UrlManager.getInsatance().getLabelVideos(), json, new HttpCallback<BaseRes<LabelVideoBean>>(this.HTTP_TAG_LABEL_VIDEO) {
                /* class com.androidx.p013lv.invention.fragment.HomeLabelFragment.C11394 */

                public void onLvSuccess(BaseRes<LabelVideoBean> baseRes) {
                    HomeLabelFragment homeLabelFragment;
                    if (HomeLabelFragment.this.loading != null && HomeLabelFragment.this.loading.isShowing()) {
                        HomeLabelFragment.this.loading.dismiss();
                    }
                    if (HomeLabelFragment.this.binding != null && (homeLabelFragment = HomeLabelFragment.this) != null) {
                        ((FragmentHomeLabelBinding) homeLabelFragment.binding).refresh.finishRefresh();
                        ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).refresh.finishLoadMore();
                        if (baseRes.getCode() == 200) {
                            if (baseRes.getData() != null && baseRes.getData().getData() != null && baseRes.getData().getData().size() > 0) {
                                HomeLabelFragment.this.mList = baseRes.getData().getData();
                                ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).recycler.setVisibility(0);
                                HomeLabelFragment.this.count = 0;
                                int adIntervalNum = AdUtils.getInstance().getAdIntervalNum(Constance.AD_HORIZONTAL);
                                if (AdUtils.getInstance().getAd(Constance.AD_HORIZONTAL) != null) {
                                    for (int i = 0; i < HomeLabelFragment.this.mList.size(); i++) {
                                        if (HomeLabelFragment.this.count == adIntervalNum) {
                                            AdInfoBean ad = AdUtils.getInstance().getAd(Constance.AD_HORIZONTAL);
                                            LabelVideoData labelVideoData = new LabelVideoData();
                                            labelVideoData.setAd(true);
                                            ArrayList arrayList = new ArrayList();
                                            arrayList.add(ad.getAdImage());
                                            labelVideoData.setCoverImg(arrayList);
                                            labelVideoData.setAdInfoBean(ad);
                                            HomeLabelFragment.this.mList.add(i, labelVideoData);
                                            HomeLabelFragment.this.count = 0;
                                        } else {
                                            HomeLabelFragment.this.count++;
                                        }
                                    }
                                }
                                if (HomeLabelFragment.this.lastId == 0) {
                                    HomeLabelFragment.this.adapterLabelVideoTwo.setNewInstance(HomeLabelFragment.this.mList);
                                    ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).refresh.resetNoMoreData();
                                    return;
                                }
                                HomeLabelFragment.this.adapterLabelVideoTwo.addData((Collection) baseRes.getData().getData());
                            } else if (HomeLabelFragment.this.lastId == 0) {
                                HomeLabelFragment.this.mList.clear();
                                HomeLabelFragment.this.mList.add(new LabelVideoData(true));
                                HomeLabelFragment.this.adapterLabelVideoTwo.setNewInstance(HomeLabelFragment.this.mList);
                                HomeLabelFragment.this.adapterLabelVideoTwo.notifyDataSetChanged();
                                ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                            } else {
                                ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                            }
                        } else if (HomeLabelFragment.this.lastId == 0) {
                            HomeLabelFragment.this.mList.clear();
                            HomeLabelFragment.this.mList.add(new LabelVideoData(true));
                            HomeLabelFragment.this.adapterLabelVideoTwo.setNewInstance(HomeLabelFragment.this.mList);
                            HomeLabelFragment.this.adapterLabelVideoTwo.notifyDataSetChanged();
                            ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                        }
                    }
                }
            });
        }
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onResume() {
        super.onResume();
    }

    public void onClick(View view) {
        if (view.getId() == C1069R.C1072id.tv_next) {
            this.tv_next.setText("下一批 >");
            this.layoutManager.smoothNextPage();
            if (this.isLast) {
                this.rv_label.scrollToPosition(0);
            }
        }
        if (!isOnClick()) {
            if (view.getId() == C1069R.C1072id.tv_new) {
                this.tv_new.setBackgroundResource(C1069R.C1071drawable.shape_label_select);
                this.tv_hot.setBackgroundResource(0);
                this.tv_like.setBackgroundResource(0);
                this.sortBy = 1;
                getLabelVideo();
            }
            if (view.getId() == C1069R.C1072id.tv_hot) {
                this.tv_new.setBackgroundResource(0);
                this.tv_hot.setBackgroundResource(C1069R.C1071drawable.shape_label_select);
                this.tv_like.setBackgroundResource(0);
                this.sortBy = 2;
                getLabelVideo();
            }
            if (view.getId() == C1069R.C1072id.tv_like) {
                this.tv_new.setBackgroundResource(0);
                this.tv_hot.setBackgroundResource(0);
                this.tv_like.setBackgroundResource(C1069R.C1071drawable.shape_label_select);
                this.sortBy = 3;
                getLabelVideo();
            }
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        AdapterLabelVideoTwo adapterLabelVideoTwo2 = this.adapterLabelVideoTwo;
        if (!(adapterLabelVideoTwo2 == null || adapterLabelVideoTwo2.getData() == null || this.adapterLabelVideoTwo.getData().size() <= 0)) {
            this.lastId = ((LabelVideoData) this.adapterLabelVideoTwo.getData().get(this.adapterLabelVideoTwo.getData().size() - 1)).getVideoId();
        }
        getLabelVideo();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.count = 0;
        this.lastId = 0;
        getLabelVideo();
    }

    @Override // androidx.fragment.app.Fragment, com.androidx.p013lv.base.p015ui.LazyFragment
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onDestroy() {
        super.onDestroy();
        HttpUtils.getInsatance().cancelTag(this.HTTP_TAG_LABEL);
        HttpUtils.getInsatance().cancelTag(this.HTTP_TAG_LABEL_VIDEO);
    }

    @Override // com.androidx.p013lv.invention.view.gridpager.PagerGridLayoutManager.PageListener
    public void onPageSizeChanged(int i) {
        this.pageTotalSize = i;
        if (1 == i) {
            this.tv_next.setText("到底啦");
            this.tv_next.setClickable(false);
        }
    }

    @Override // com.androidx.p013lv.invention.view.gridpager.PagerGridLayoutManager.PageListener
    public void onPageSelect(int i) {
        if (i + 1 == this.pageTotalSize) {
            this.tv_next.setText("< 到底啦");
            this.isLast = true;
            return;
        }
        this.tv_next.setText("下一批 >");
        this.isLast = false;
    }

    /* renamed from: com.androidx.lv.invention.fragment.HomeLabelFragment$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void goTop() {
            if (!HomeLabelFragment.this.isOnClick()) {
                ((FragmentHomeLabelBinding) HomeLabelFragment.this.binding).recycler.scrollToPosition(0);
            }
        }
    }
}
